/*
	NAME : RUSHIKESH M FANSE
	ROLL NO : 16
	DIV : H
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#define MAX 20
using namespace std;
int tracks,curr_head,h_pos_f,h_pos_s,method,track_traversed,avg_seek_len,seek_length,start;
int a[MAX],b[MAX];
void FIFO()
{
	 int i;
	 int k=0;
	 for(i=0;i<tracks;i++)
	 {
	        track_traversed+= (a[i]>curr_head)?(a[i]-curr_head):(curr_head-a[i]);
	        curr_head =a[i];
		if(curr_head!=start)
	       		k++;
	 }
	 cout<<"track traversed : "<<track_traversed<<" "<<"average seek length : "<<(track_traversed*1.0)/k<<"\n";
}

void SSTF()
{
	int i,j,k;
	for(j=0;start>=b[j];j++);
	i=j-1;
	if((start-b[i])<(b[j]-start))
	{
		for(int k=i;k>=0;k--)
		{
			track_traversed+=curr_head-b[k];
			curr_head=b[k];
			
		}
		for(int k=j;k<tracks;k++)
		{
			track_traversed+=b[k]-curr_head;
			curr_head=b[k];
		}
	}
	else if((start-b[i])>(b[j]-start))
	{
		
		for(int k=j;k<tracks;k++)
		{
			track_traversed+=b[k]-curr_head;
			curr_head=b[k];
		}
		
		for(int k=i;k>=0;k--)
		{
			track_traversed+=curr_head-b[k];
			curr_head=b[k];
		}
	}
	cout<<"track traversed : "<<track_traversed<<" "<<"average seek length : "<<(track_traversed*1.0)/tracks<<"\n";
}

void SCAN()
{
	int sc,i,j;
	cout<<"enter the direction in which scanning is to be done\n";
	cout<<"1.left\n2.right\n";
	cin>>sc;
	for(j=0;start>=b[j];j++);
	i=j-1;

	if(sc==1)
	{
		for(int k=i;k>=0;k--)
		{
			track_traversed+=curr_head-b[k];
			curr_head=b[k];
			
		}
		for(int k=j;k<tracks;k++)
		{
			track_traversed+=b[k]-curr_head;
			curr_head=b[k];
		}
	}
	else if(sc==2)
	{
		
		for(int k=j;k<tracks;k++)
		{
			track_traversed+=b[k]-curr_head;
			curr_head=b[k];
		}

		for(int k=i;k>=0;k--)
		{
			track_traversed+=curr_head-b[k];
			curr_head=b[k];
		}
	}
	cout<<"track traversed : "<<track_traversed<<" "<<"average seek length : "<<(track_traversed*1.0)/tracks<<"\n";
	
}

void CSCAN()
{

	int sc,i,j;
	cout<<"enter the direction in which scanning is to be done\n";
	cout<<"1.left\n2.right\n";
	cin>>sc;
	for(j=0;start>=b[j];j++);
	i=j-1;

	if(sc==1)
	{
		for(int k=0;k<=i;k++)
		{
			track_traversed+=b[k]-curr_head;
			curr_head=b[k];
		}
		for(int k=j;k<tracks;k++)
		{
			track_traversed+=b[k]-curr_head;
			curr_head=b[k];
			
		}
	}
	else if(sc==2)
	{
		
		for(int k=j;k<tracks;k++)
		{
			track_traversed+=b[k]-curr_head;
			curr_head=b[k];
			
		}
			
		for(int l=0;l<=i;l++)
		{
	       		 track_traversed+= (b[l]>curr_head)?(b[l]-curr_head):(curr_head-b[l]);
			curr_head=b[l];
		}
	}
	cout<<"track traversed : "<<track_traversed<<" "<<"average seek length : "<<(track_traversed*1.0)/tracks<<"\n";
}
int main()
{
	cout<<"Enter the no of tracks\n";
	cin>>tracks;
	cout<<"enter the tracks\n";
	for(int i=0;i<tracks;i++)
	{
		cin>>b[i];
		a[i]=b[i];
	}
	sort(b,b+tracks);
	cout<<"which is current head\n";
	cin>>curr_head;
	start=curr_head;
	for(int i=0;i<tracks;i++)
		if(b[i]==curr_head)
			h_pos_s=i;
	while(method<=4)
	{
		track_traversed=0;
		avg_seek_len=0;
		curr_head=start;
		switch(method)
		{
			case 1:
				//FIFO
				cout<<"FIFO\n";
				FIFO();	
			break;
			case 2:
				//SSTF
				cout<<"SSTF\n";
				SSTF();
			break;
			case 3:
				//SCAN
				cout<<"SCAN\n";
				SCAN();
			break;
			case 4:
				//CSCAN
				cout<<"CSCAN\n";
				CSCAN();
			break;
		}
		method++;
	}
		
}
/*
OUTPUT:
rishi@rishi-PC:~$ ./a.out
Enter the no of tracks
9
enter the tracks
55 58 39 18 90 160 150 38 184
which is current head
100
FIFO
track traversed : 498 average seek length : 55.3333
SSTF
track traversed : 248 average seek length : 27.5556
SCAN
enter the direction in which scanning is to be done
1.left
2.right
2
track traversed : 250 average seek length : 27.7778
CSCAN
enter the direction in which scanning is to be done
1.left
2.right
2
track traversed : 322 average seek length : 35.7778
*/
